
clear
set more off
tempfile temp
set matsize 600


// N counts
/**************************************************/
use $wd/cps/cps_wd.dta if  earnsamp & wage!=.  & union!=., clear

cap drop if ${area}==.
keep if alloc!=1

// Shares not formed yet to
// Account sample size considerations later
g unit=1
collapse (rawsum) unit wgt,by(year year2 ${ind} ${area} union)
collapse (mean) unit wgt,by(year2 ${ind} ${area} union)

egen id=group(${ind} ${area} year2)
reshape wide unit wgt,i(id) j(union)
drop id

ren unit0 sample_ic0
ren unit1 sample_ic1
ren wgt0 N_ic0
ren wgt1 N_ic1	

replace sample_ic0=0 if sample_ic0==.
replace sample_ic1=0 if sample_ic1==.
replace N_ic0=0 if N_ic0==.
replace N_ic1=0 if N_ic1==.		

g sample_ic=sample_ic0+sample_ic1
g N_ic=N_ic0+N_ic1
		
*save ${wd}/keyvars/N_counts/N.dta, replace



// Union Terms
/**************************************************/


// Union proportion i-c-t
preserve
	g propic = N_ic1/N_ic
	keep propic ${ind} ${area} year2
	save ${wd}/keyvars/propic.dta, replace 
restore

/*
// Union proportion c-t
preserve
	collapse (rawsum) N_ic1 N_ic0,by(${area} year2)
	g p_cu = N_ic1/(N_ic1+N_ic0)
	keep p_cu ${area} year2
	save ${wd}/keyvars/union_prop/propc.dta, replace 
restore

// Predicted Union proportion c-t
preserve
	keep ${ind} ${area} year2 N_ic0 N_ic1

	bys ${ind} year2: egen N_i1=total(N_ic1)
	bys ${ind} year2: egen N_i0=total(N_ic0)

	egen id=group(${area} ${ind})
	tsset id year2

	g pN_ic0=L.N_ic0*(N_i0/L.N_i0)
	g pN_ic1=L.N_ic1*(N_i1/L.N_i1)		

	collapse (rawsum) pN_ic0 pN_ic1,by(${area} year2)
	g phat_c=pN_ic1/(pN_ic1+pN_ic0)

	drop if year2==1

	keep ${area} year2 phat_c

	save ${wd}/keyvars/union_prop/pred_propc.dta, replace 
restore

*/

/*
// Save National Level Counts
collapse (rawsum) sample_ic N_ic,by(${ind} year2)
ren sample_ic sample_i
ren N_ic N_i

save ${wd}\keyvars\N_counts\Ni.dta, replace
*/
